package com.joonggo.gip.controls.user;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.joonggo.gip.controls.ControllerTemplate;
import com.joonggo.gip.controls.ExecuteDetailFuncDetailStatement;
import com.joonggo.gip.dao.UserDao;
import com.joonggo.gip.service.UserService;
import com.joonggo.gip.vo.Group;
import com.joonggo.gip.vo.User;

/* DispatcherServlet(Front Controller)의 디폴트 전략 중 
 HandlerMapping 전략, HandlerAdapter 전략 이 있는데 
 HandlerMapping 전략은 DefaultAnnotationHandlerMapping 전략
 HandlerAdapter 전략은 AnnotationMethodHandlerAdapter 전략 을 사용하여
 이는 DispatcherServlet의 기본 핸들러 어댑터 전략이므로 따로 설정해줄 필요가 없다.
 특정한 컨트롤러 인터페이스를 구현하지 않고도 메소드 레벨별, 클래스 레벨별로 하나의 컨트롤러를 만들 수 있다.
 또 다른 특징은 컨트롤러 하나가 하나 이상의 URL에 매핑될 수 있다. 
 
-> 여기서 HandlerMapping의 전략이란?
 : 핸들러 매핑은 HTTP 요청정보를 이용해서 이를 처리할 핸들러 오브젝트, 즉 컨트롤러를 찾아주는 기능을 가진 
 DispatcherServlet의 전략이다. 
 */

@Controller
@RequestMapping("/Login/hr")
@SessionAttributes("loginUser")
public class UserController {
	@Autowired	UserService userService;
	@Autowired	UserDao userDao;
	@Autowired ControllerTemplate mainFunc;
    
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

	
	@RequestMapping(value="/loginSuccess",method=RequestMethod.POST)
	public HashMap<String,Object> loginSuccess(final User user, final ServletRequest request) throws Exception {
		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {
				HttpServletRequest hReq = (HttpServletRequest) request;
				hReq.getSession().setAttribute("loginedUser", user);
			}
		});
	}
	
	@RequestMapping(value="/logout",method=RequestMethod.POST)
	public HashMap<String,Object> logout(final ServletRequest request) throws Exception {
		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {
				HttpServletRequest hReq = (HttpServletRequest) request;
				hReq.getSession().removeAttribute("loginedUser");
			}
		});
	}
	
	@RequestMapping(value="/getUser",method=RequestMethod.POST)
	@ResponseBody
	public HashMap<String,Object> getUser(final String user_Id) throws Exception {
		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {
				responseResult.put("user", userDao.getUser(user_Id));
			}
		});
	}

	@RequestMapping(value="/addUser",method=RequestMethod.POST)
	@ResponseBody
	public HashMap<String,Object> add(final User user, BindingResult result) throws Exception {
		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {
				userService.addUser(user);
			}
		});
	}
	@RequestMapping(value="/deleteUser",method=RequestMethod.GET)
	@ResponseBody
	public HashMap<String,Object> getGroupByNum(final ServletRequest request) throws Exception {

		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {

				HttpServletRequest req = (HttpServletRequest)request;
				HttpSession session= req.getSession();
				User loginedUser=(User)session.getAttribute("loginedUser");

				System.out.println(loginedUser.getUser_Id());
				int count = userService.removeUser(loginedUser.getUser_Id());
				
				if (count > 0) {
					responseResult.put("status", "success");
				} else {
					responseResult.put("status", "fail");
					responseResult.put("message", "해당 회원이 없습니다!");
				}
			}
		});
	}
	private void printUser(User user) {
		System.out.println("User Information... ----- ");
		System.out.println("ID : " + user.getUser_Id());
		System.out.println("PASS : " + user.getUser_Pass());
		System.out.println("NAME : " + user.getUser_Name());
		System.out.println("KEYWORD : " + user.getUser_Keyword());
		System.out.println("MONEY : " + user.getUser_Money());
		System.out.println("PHONE : " + user.getUser_Phone());
	}
}

